"""
@Project:efficient
@File:serializer.py
@Author:韩晓雷
@Date:16:03
"""
import re
from rest_framework import serializers
from rest_framework.validators import ValidationError
from flow.models import *
from user.models import *


# 审批层级
class LeveSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField(read_only=True)

    def get_user_name(self, obj):
        return obj.user.name

    class Meta:
        model = LevelModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$", name):
            raise ValidationError(f'{name} 名称由2到30位汉字组成！')

        return attrs


# 类别
class TypeSer(serializers.ModelSerializer):
    lastervisename = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    lastervisename_name = serializers.SerializerMethodField(read_only=True)

    def get_lastervisename_name(self, obj):
        return obj.lastervisename.name

    class Meta:
        model = TypeModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{1,30}$", name):
            raise ValidationError(f'{name} 名称由1到30位汉字、数字、字母或下划线组成！')

        return attrs


# 流程
class FlowSer(serializers.ModelSerializer):
    dept = serializers.PrimaryKeyRelatedField(queryset=DeptModel.objects.all())
    dept_name = serializers.SerializerMethodField()

    def get_dept_name(self, obj):
        return obj.dept.name

    class Meta:
        model = FlowModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$", name):
            raise ValidationError(f'{name} 名称由2到30位汉字、数字、字母或下划线组成！')

        return attrs


# 流程节点
class FlowNodeSer(serializers.ModelSerializer):
    vettype = serializers.PrimaryKeyRelatedField(queryset=TypeModel.objects.all())
    vettype_name = serializers.SerializerMethodField(read_only=True)

    def get_vettype_name(self, obj):
        return obj.vettype.name

    flow = serializers.PrimaryKeyRelatedField(queryset=FlowModel.objects.all())
    flow_name = serializers.SerializerMethodField()

    def get_flow_name(self, obj):
        return obj.flow.flow_name

    name = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    name_name = serializers.SerializerMethodField(read_only=True)

    def get_name_name(self, obj):
        return obj.name.name

    level = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    level_name = serializers.SerializerMethodField(read_only=True)

    def get_level_name(self, obj):
        return obj.level.name

    class Meta:
        model = FlowNodeModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("node_name")
        examination_why = attrs.get("examination_why")
        why = attrs.get("why")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$", name):
            raise ValidationError(f'{name} 名称由2到30位汉字、数字、字母或下划线组成！')

        if not re.findall(r"^[\u4e00-\u9fa5\w]{3, 50}", examination_why):
            raise ValidationError(f'{examination_why} 审批原因由3到50、数字、字母或下划线组成！')

        if not re.findall(r"^[\u4e00-\u9fa5\w]{3, 50}", why):
            raise ValidationError(f'{why} 请求由3到50、数字、字母或下划线组成！')

        return attrs


# 用户流程节点
class UserFlowNodeSer(serializers.ModelSerializer):
    name = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    name_name = serializers.SerializerMethodField(read_only=True)

    def get_name_name(self, obj):
        return obj.name.name

    flow_node = serializers.PrimaryKeyRelatedField(queryset=FlowNodeModel.objects.all())
    flow_node_name = serializers.SerializerMethodField(read_only=True)

    def get_flow_node_name(self, obj):
        return obj.flow_node.name

    class Meta:
        model = UserFlowNodeModel
        fields = "__all__"


# 节点信息
class NodeInformationSer(serializers.ModelSerializer):
    flow_node = serializers.PrimaryKeyRelatedField(queryset=FlowNodeModel.objects.all())
    flow_node_name = serializers.SerializerMethodField(read_only=True)

    def get_flow_node_name(self, obj):
        return obj.flow_node.name

    class Meta:
        model = NodeInformationModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("describe")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$", name):
            raise ValidationError(f'{name} 描述由2到30位汉字、数字、字母或下划线组成！')

        return attrs


# 历史审批
class HistoryExaminationSer(serializers.ModelSerializer):
    flow_node = serializers.PrimaryKeyRelatedField(queryset=FlowNodeModel.objects.all())
    flow_node_name = serializers.SerializerMethodField(read_only=True)

    def get_flow_node_name(self, obj):
        return obj.flow_node.name

    class Meta:
        model = HistoryExaminationModel
        fields = "__all__"


# 模板类型
class TemplateSer(serializers.ModelSerializer):
    type = serializers.PrimaryKeyRelatedField(queryset=FlowModel.objects.all())
    type_name = serializers.SerializerMethodField(read_only=True)

    def get_type_name(self, obj):
        return obj.type.flow_name

    class Meta:
        model = Template
        fields = "__all__"

    def validate(self, attrs):
        template_name = attrs.get("template_name")
        if not re.findall(r"[\u4e00-\u9fa5]{1,9}", template_name):
            raise ValidationError(f'{template_name}名称由2到9为汉字组成')
        return attrs


# 请假类型
class LeaveTypeSer(serializers.ModelSerializer):
    type = serializers.PrimaryKeyRelatedField(queryset=Template.objects.all())
    type_name = serializers.SerializerMethodField(read_only=True)

    def get_type_name(self, obj):
        return obj.type.template_name

    class Meta:
        model = Leave_Type
        fields = "__all__"

    def validate(self, attrs):
        type_name = attrs.get("type_name")
        if not re.findall(r"[\u4e00-\u9fa5]{1,9}", type_name):
            raise ValidationError(f'{type_name}名称由2到9为汉字组成')
        return attrs


# 请假模板cause begin_time end_time end_time type
class LeaveSer(serializers.ModelSerializer):
    class Meta:
        model = Leave
        fields = "__all__"


# 借款模板begin_time cause money type
class MoneySer(serializers.ModelSerializer):
    class Meta:
        model = Money
        fields = "__all__"


# 出差模板cause begin_time end_time address peer type
class TravelSer(serializers.ModelSerializer):
    class Meta:
        model = Travel
        fields = "__all__"


# 转正模板 begin_time cause type
class FormalSer(serializers.ModelSerializer):
    class Meta:
        model = Formal
        fields = "__all__"


# 用车模板 begin_time end_time start end vehicle plate type begin_time cause type
class CarSer(serializers.ModelSerializer):
    class Meta:
        model = Car
        fields = "__all__"


# 报销模板voucher type  cause money
class ExpenseSer(serializers.ModelSerializer):
    class Meta:
        model = Expense
        fields = "__all__"


class VehicleSer(serializers.ModelSerializer):
    class Meta:
        model = Vehicle
        fields = "__all__"


class PlateSer(serializers.ModelSerializer):
    class Meta:
        model = Plate
        fields = "__all__"


class StuffSer(serializers.ModelSerializer):
    class Meta:
        model = StuffModel
        fields = "__all__"


class MailSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_name = serializers.SerializerMethodField(read_only=True)

    def get_user_name(self, obj):
        return obj.user.name

    users = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    users_name = serializers.SerializerMethodField(read_only=True)

    def get_users_name(self, obj):
        return obj.users.name

    class Meta:
        model = MailModel
        fields = "__all__"

